﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PSDLib.Models;

namespace PSDManager
{
    public partial class TaskInfo : UserControl
    {
        private const int channelWidth = 210;
        private const int channelHeight = 90;

        public event EventHandler OnDownloadStateChanged;

        private ChannelInfo currChannel;

        public dl_task Task { get; private set; }
        public List<dl_task_channel> Channels { get; private set; }

        public ManagerHelper ManagerHelper { get; set; }

        public TaskInfo()
        {
            InitializeComponent();

            this.palChannelList.MouseEnter += TaskInfo_MouseEnter;
        }

        private void TaskInfo_MouseEnter(object sender, EventArgs e)
        {
            if (currChannel != null)
            {
                currChannel.ShowButtons(false);
                currChannel.BackColor = Color.White;
            }
        }

        public void Reset()
        {
            try
            {
                lblRoom.Text = "";
                palChannelList.Controls.Clear();
            }
            catch
            { }
        }

        public void SetTaskInfo(dl_task task, List<dl_task_channel> channels)
        {
            if (Task != null && Task.id != task.id)
            {
                palChannelList.Controls.Clear();
            }

            Task = task;
            Channels = channels;

            lblRoom.Text = Task.task_name;

            for (int i = 0; i < channels.Count; i++)
            {
                ChannelInfo ch;
                var ctrl = palChannelList.Controls.Find("channelInfo_" + channels[i].id, false);

                if (ctrl.Length > 0)
                {
                    ch = (ChannelInfo)ctrl[0];
                }
                else
                {
                    ch = new ChannelInfo();
                    ch.Width = channelWidth;
                    ch.Height = channelHeight;
                    //ch.BorderStyle = BorderStyle.FixedSingle;
                    ch.Name = "channelInfo_" + channels[i].id;
                    ch.OnDownloadStateChanged += Ch_OnDownloadStateChanged;
                    ch.OnMouseEnter += Ch_OnMouseEnter;
                    palChannelList.Controls.Add(ch);
                }

                ch.ManagerHelper = ManagerHelper;
                ch.SetChannel(Task, channels[i]);

                SetChannelPos(ch, i);
            }
        }

        private void Ch_OnMouseEnter(object sender, EventArgs e)
        {
            if (currChannel != null)
            {
                currChannel.ShowButtons(false);
                currChannel.BackColor = Color.White;
            }

            currChannel = (sender as ChannelInfo);

            currChannel.ShowButtons(true);

            currChannel.BackColor = Color.AliceBlue;
        }

        private void Ch_OnDownloadStateChanged(object sender, EventArgs e)
        {
            OnDownloadStateChanged?.Invoke(sender, e);
        }

        private void SetChannelPos(ChannelInfo ch, int index)
        {
            int paddingLeft = 10;
            int paddingRight = 10;
            int paddingBottom = 15;
            int colMax = Convert.ToInt32(Math.Floor(Convert.ToDouble(palChannelList.Width - palChannelList.Padding.Left - palChannelList.Padding.Right) / (channelWidth + paddingLeft + paddingRight)));
            colMax = colMax == 0 ? 1 : colMax;

            int col;
            int row = Math.DivRem(index, colMax, out col);

            ch.Left = palChannelList.Padding.Left + col * channelWidth + paddingLeft;
            ch.Top = palChannelList.Padding.Top + row * channelHeight + row * paddingBottom;

            this.Height = ch.Top + channelHeight + palBottom.Height + palTask.Height + 10;
        }

        private void palChannelList_Resize(object sender, EventArgs e)
        {
            for (int i = 0; i < palChannelList.Controls.Count; i++)
            {
                SetChannelPos((ChannelInfo)palChannelList.Controls[i], i);
            }
        }
    }
}
